
import 'package:dio/dio.dart';

import '../../utils/string_tool.dart';
import '../request/response_data.dart';

/// 动态接口调用的返回结果处理
/// 非正常的返回均在此拦截器中处理
/// 使用时：var data = Api.get(xxx);
/// 根据接口返回参数的类型直接使用
class ResultInterceptor extends InterceptorsWrapper {

  @override
  void onResponse(Response response, ResponseInterceptorHandler handler) {
    // TODO: implement onResponse
    //   if(response.requestOptions.method.toLowerCase()=='post'){
    //     ApiClientManager.postPaths.remove(response.requestOptions.path);
    //   }
    //   if(response.requestOptions.method.toLowerCase()=='put'){
    //     ApiClientManager.putPaths.remove(response.requestOptions.path);
    //   }
    //   if(response.requestOptions.method.toLowerCase()=='delete'){
    //     ApiClientManager.deletePaths.remove(response.requestOptions.path);
    //   }

      StringTool.long_p("****response****${DateTime.now()}");
      StringTool.long_p("****url****"+response.requestOptions.baseUrl+response.requestOptions.path);
      StringTool.long_p("****data****"+response.data.toString());
      // 如果是非json格式的返回结果，则直接跳过拦截器
      if (response.requestOptions.responseType != ResponseType.json) {
        handler.next(response);
      }
      int? httpStatus = response.statusCode;
      String requestPath = response.requestOptions.path;
      int returnCode = -1;
      String codeMsg='';
      if (response.data != null && response.data is Map) {
        returnCode = response.data["code"];
        codeMsg = response.data["message"]?? '';
      }
      bool success = false;
      if (httpStatus == 200) {
        success = true;
      } else {
        response.data = ResponseData(success, httpStatus!, response.statusMessage!, null, path: requestPath);
        handler.next(response);
      }
      response.data=ResponseData(success, returnCode, codeMsg,response.data!=null&& response.data is Map? response.data["result"]:null, path: requestPath,total: response.data["total"],token: response.data['token'],rows: response.data["rows"]);
      handler.next(response);
  }

}
